//
//  DKSetNewPwdController.m
//  DanKU
//
//  Created by 吴斌 on 2023/11/8.
//

#import "DKSetNewPwdController.h"
#import "DKLoginRegisterNameView.h"
#import "DKLoginAccountView.h"
#import "DKLoginPwdView.h"
#import "DKLoginViewController.h"
@interface DKSetNewPwdController ()
@property (nonatomic, strong)DKLoginAccountView * dk_LoginAccountView;
@property (nonatomic, strong)DKLoginPwdView * dk_LoginPwdView;
@property (nonatomic, strong)UIButton * dk_RegisterNextBtn;

@end

@implementation DKSetNewPwdController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.whiteColor;
    [self dk_creatView];
    [self makeNavBack];
}
- (void)dk_creatView{
    WeakSelf(self)
    //log名称
    DKLoginRegisterNameView *dk_changePwdHeadView = [[DKLoginRegisterNameView alloc] init];
    [self.view addSubview:dk_changePwdHeadView];
    [dk_changePwdHeadView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@(DK_IsWidthScale(0)));
        make.top.equalTo(@(DK_IsWidthScale(76)+DK_SafeAreaTop));
        make.height.equalTo(@(DK_IsWidthScale(40)));
    }];
    //标题
    UILabel *dk_changePwdHeadLab = [[UILabel alloc] init];
    dk_changePwdHeadLab.textColor = DK_Hex(@"#333333");
    dk_changePwdHeadLab.font = DK_FONT_Medium(17);
    dk_changePwdHeadLab.text = @"Please enter a new password";
    [self.view addSubview:dk_changePwdHeadLab];
    [dk_changePwdHeadLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(dk_changePwdHeadView.mas_bottom).offset(DK_IsWidthScale(30));
        make.height.equalTo(@(DK_IsWidthScale(24)));
        make.left.equalTo(@(DK_IsWidthScale(30)));
    }];
    //账号
    self.dk_LoginAccountView = [[DKLoginAccountView alloc] init];
    self.dk_LoginAccountView.smsType = @"reset_pwd";
    self.dk_LoginAccountView.dk_LoginAccountTextField.text = [DKGlobalHelper defaultManager].userBaseInfoMdl.phone;
    self.dk_LoginAccountView.isHidSend = YES;
    self.dk_LoginAccountView.dk_LoginAccountTextField.enabled = NO;
    [self.view addSubview:self.dk_LoginAccountView];
    [self.dk_LoginAccountView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@(DK_IsWidthScale(30)));
        make.right.right.equalTo(@(-DK_IsWidthScale(30)));
        make.top.equalTo(dk_changePwdHeadLab.mas_bottom).offset(DK_IsWidthScale(20));
        make.height.equalTo(@(DK_IsWidthScale(50)));
    }];
    self.dk_LoginAccountView.goCheckAcountBlock = ^(BOOL isPhoneNull) {//检查输入手机号是否是空
        [weakSelf makeNextStatus];
    };
    //输入密码
    self.dk_LoginPwdView = [[DKLoginPwdView alloc] init];
    [self.view addSubview:self.dk_LoginPwdView];
    [self.dk_LoginPwdView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(30)));
        make.right.equalTo(@(-DK_IsWidthScale(30)));
        make.top.equalTo(self.dk_LoginAccountView.mas_bottom).offset(DK_IsWidthScale(20));
        make.height.equalTo(@(DK_IsWidthScale(50)));
    }];
    self.dk_LoginPwdView.goCheckLoginPwdBlock = ^(BOOL isPwdNull) {//检查输入密码是否为空
        [weakSelf makeNextStatus];
    };
    
    //协议
    UIButton * dk_AuthPrivacyAgreementBtn = [[UIButton alloc] init];
    [dk_AuthPrivacyAgreementBtn setTitle:@"《Privacy Agreement》" forState:UIControlStateNormal];
    [dk_AuthPrivacyAgreementBtn setTitleColor:DK_Hex(@"#CCCCCC") forState:UIControlStateNormal];
    dk_AuthPrivacyAgreementBtn.titleLabel.font = DK_FONT_Regular(12);
    [dk_AuthPrivacyAgreementBtn addTarget:self action:@selector(checkPrivacyAgreement) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:dk_AuthPrivacyAgreementBtn];
    [dk_AuthPrivacyAgreementBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(@(-DK_IsWidthScale(46)-DK_SafeAreaBottom));
        make.height.equalTo(@(DK_IsWidthScale(17)));
    }];
    //next
    self.dk_RegisterNextBtn = [[UIButton alloc] init];
    [self.dk_RegisterNextBtn setTitle:@"Next" forState:UIControlStateNormal];
    [self.dk_RegisterNextBtn setTitleColor:DK_Hex(@"#4566FF") forState:UIControlStateDisabled];
    [self.dk_RegisterNextBtn setTitleColor:DK_Hex(@"#FFFFFF") forState:UIControlStateNormal];
    self.dk_RegisterNextBtn.titleLabel.font = DK_FONT_Semibold(16);
    [self.dk_RegisterNextBtn setBackgroundImage:DK_GetImage(@"dk_registerNextSelectBg") forState:UIControlStateNormal];
    [self.dk_RegisterNextBtn setBackgroundImage:DK_GetImage(@"dk_registerNextBg") forState:UIControlStateDisabled];
    self.dk_RegisterNextBtn.enabled = NO;
    [self.dk_RegisterNextBtn addTarget:self action:@selector(goCheck) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.dk_RegisterNextBtn];
    [self.dk_RegisterNextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(30)));
        make.right.equalTo(@(-DK_IsWidthScale(30)));
        make.height.equalTo(@(DK_IsWidthScale(50)));
        make.bottom.equalTo(dk_AuthPrivacyAgreementBtn.mas_top).offset(-DK_IsWidthScale(97));
    }];
}
- (void)checkPrivacyAgreement{
    WeakSelf(self)
    DKWebViewController * dk_WebViewCtrl = [[DKWebViewController alloc] initWithUrl:DK_PrivacyAgreement withIsNavBar:YES];
    [weakSelf.navigationController pushViewController:dk_WebViewCtrl animated:YES];
}
- (void)makeNextStatus{
    if(self.dk_LoginAccountView.dk_LoginAccountTextField.text.length > 0&&([self.dk_LoginPwdView getLoginPwd].length > 0)){
        self.dk_RegisterNextBtn.enabled = YES;
    }else{
        self.dk_RegisterNextBtn.enabled = NO;
    }
}
- (void)goCheck{
    NSMutableDictionary *checkPwdDic = [[NSMutableDictionary alloc] init];
    [checkPwdDic setValue:self.dk_LoginAccountView.dk_LoginAccountTextField.text forKey:@"loginName"];
    [checkPwdDic setValue:self.upPasswordCertificate forKey:@"upPasswordCertificate"];
    NSLog(@"密码:%@",[self.dk_LoginPwdView getLoginPwd]);
    NSLog(@"公钥:%@",[DKGlobalHelper defaultManager].userRsaKey);
    NSLog(@"公钥加密：%@",[IPNRSAUtil encryptString:[self.dk_LoginPwdView getLoginPwd] publicKey:[DKGlobalHelper defaultManager].userRsaKey]);
    [checkPwdDic setValue:[IPNRSAUtil encryptString:[self.dk_LoginPwdView getLoginPwd] publicKey:[DKGlobalHelper defaultManager].userRsaKey] forKey:@"password"];
    [DKPublicHttp apiResetPwdForAPI:checkPwdDic complete:^{
        [DKAlertHelper showMessage:@"reset password sucess!"];
        //返回登录页面
        DKLoginViewController * dk_LoginViewCtrl = [[DKLoginViewController alloc] init];
        UINavigationController *dk_LoginViewNavCtrl = [[UINavigationController alloc] initWithRootViewController:dk_LoginViewCtrl];
        ((AppDelegate*)([UIApplication sharedApplication].delegate)).window.rootViewController = dk_LoginViewNavCtrl;
    }];
}
@end
